M4=$(shell which m4)
MAKEFILE_TEMPLATE=Makefile.m4
ARCHIVE_TARBALL='m4cfg-$(shell date '+%Y-%M-%d_%H%m').tar.xz'
EXPORT_FILE=export.txt

HOSTS_FILE=hosts.txt
BASE_FILE=base.m4

# Only speak when requested.
ifndef VERBOSE
.SILENT:
endif

ifndef HOST
HOST=$(shell hostname)
endif

export

.PHONY: all
all:
# If a configuration name is provided, build it and nothing else
	if [ ! -z '$(NAME)' ]; then \
		make -C '$(NAME)/' all; \
	else \
		if [ ! -f '$(EXPORT_FILE)' ]; then \
			echo "ghay'cha'! There don't appear to be any configurations"; \
			exit 1; \
		fi; \
		while read -r cfg; do \
			make -C "$$cfg/" all; \
		done < '$(EXPORT_FILE)'; \
	fi
.PHONY: allhosts
allhosts:
	while read -r cfg; do \
		make -C "$$cfg/" allhosts; \
	done < '$(EXPORT_FILE)'

.PHONY: addme
addme:
	if [ -z '$(NAME)' ]; then \
		echo 'No configuration name given. Use'; \
		echo '    make NAME="..." $@'; \
	fi
	make -C '$(NAME)/' $@

.PHONY: addcfg
addcfg:
	if [ -z '$(NAME)' ]; then \
		echo 'No configuration name given. Use'; \
		echo '    make NAME="..." ... $@'; \
		exit 1; \
	fi

	if [ -z '$(SOURCE)' ]; then \
		if [ -z '$(OUTPUT)' ]; then \
			echo 'No output file specified. Use'; \
			echo '    make OUTPUT="..." ... $@'; \
			exit 1; \
		fi; \
		if [ '$(FORCE)' = '1' ]; then \
			rm -rvf '$(NAME)/'; \
		fi; \
		if [ -d '$(NAME)/' ]; then \
			echo -e 'A configuration named "$(NAME)" already exists.'; \
			echo -e 'If you want to overwrite this configuration, use (with caution)'; \
			echo -e '    make NAME="$(NAME)" FORCE=1 $@'; \
			exit 1; \
		else \
			mkdir -v '$(NAME)/'; \
		fi; \
		$(M4) -D '_NAME'='$(NAME)' \
		      -D '_OUTPUT'='$(OUTPUT)' \
		< $(MAKEFILE_TEMPLATE) > '$(NAME)/Makefile'; \
		touch '$(NAME)/$(HOSTS_FILE)'; \
		touch '$(NAME)/$(BASE_FILE)'; \
	else \
		# Source directly from a tarball \
		tar -xvf '$(SOURCE)'; \
	fi
	echo '$(NAME)' >> '$(EXPORT_FILE)'

.PHONY: link
link:
	if [ -z '$(NAME)' ]; then \
		echo 'No configuration name given. Use'; \
		echo '    make NAME="..." ... $@'; \
		exit 1; \
	fi

	if [ -z '$(DEST)' ]; then \
		echo 'No destination specified. Use'; \
		echo '    make DEST="..." ... $@'; \
		exit 1; \
	fi

	make -C '$(NAME)' DEST='$(DEST)' link 

.PHONY: rmcfg
rmcfg:
	if [ -z '$(NAME)' ]; then \
		echo 'No configuration name given. Use'; \
		echo '    make NAME="..." ... $@'; \
		exit 1; \
	fi

	if [ -d '$(NAME)' ]; then rm -rvf '$(NAME)'; fi

	echo 'Removing $(NAME) from export targets'
	touch '$(EXPORT_FILE).tmp'
	cat '$(EXPORT_FILE)' | grep '[^\($(NAME)\)]' > '$(EXPORT_FILE).tmp' || :
	mv '$(EXPORT_FILE).tmp' '$(EXPORT_FILE)'

.PHONY: archive
archive:
	if [ -z "$(NAME)" ]; then \
		tar -cjvf '$(ARCHIVE_TARBALL)' -T '$(EXPORT_FILE)' --add-file 'export.txt'; \
	else \
		tar -cjvf '$(NAME).tar.xz' '$(NAME)/'; \
	fi

.PHONY: purge
purge:
	if [ -z '$(FORCE)' ]; then \
		echo 'WARNING'; \
		echo 'This action would completely purge all configurations.'; \
		echo 'If you truly wish to proceed, run this again with "FORCE=1"'; \
	else \
		while read -r cfg; do \
			rm -rvf "$$cfg/"; \
		done < '$(EXPORT_FILE)'; \
		rm -v '$(EXPORT_FILE)'; \
	fi

.PHONY: clean
clean:
	cat '$(EXPORT_FILE)' | sort | uniq > '$(EXPORT_FILE).tmp'
	mv '$(EXPORT_FILE).tmp' '$(EXPORT_FILE)'

	while read -r cfg; do \
		make -C "$$cfg/" clean; \
	done < '$(EXPORT_FILE)'
